#include "student_mannager.h"

int addStudent()
{
    drawAddStudent();
    moveCursor('r',2);
    std::string new_username, new_sexual, new_userid;
    std::cout << "\033[01;32m";
    new_userid = readCommand();
    if(new_userid == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',22);
    new_username = readCommand();
    if(new_username == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',41);
    new_sexual = readCommand();
    if(new_sexual == "q") return -1;
    std::cout << "\033[0m";
    if(new_username == "" || (new_sexual != "male" && new_sexual != "female") || new_userid == "") return 0;
    if(Stu_isValid(new_userid, new_username))
    {
        std::fstream file;
        file.open(STU_PATH, std::ios::out|std::ios::app);
        std::string addline;
        addline.append("\"");
        addline.append(new_userid);
        addline.append("\" \"");
        addline.append(new_username);
        addline.append("\" \"");
        addline.append(new_sexual);
        addline.append("\" \"\" \"0\"");
        file << addline << std::endl;
        return 1;
    }
    else return 0;
}

int deleteStudent()
{
    bool isok = 0;
    drawDeleteStudent();
    moveCursor('r', 12);
    std::cout << "\033[01;32m";
    std::string userid;
    userid = readCommand();
    if(userid == "q") return -1;
    std::cout << "\033[0m";
    std::fstream in, out;
    in.open(STU_PATH);
    std::vector<std::string> ret;
    while(!in.eof())
    {
        std::vector<std::string> temp;
        std::string line;
        getline(in, line);
        if(line.empty()) continue;
        temp = split(line, ' ');
        if(temp[0] == userid)
        {
            isok = 1;
        }
        else ret.push_back(line);
    }
    if(isok)
    {
        out.open(STU_PATH, std::ios::out);
        for(auto a : ret) out << a << std::endl;
    }
    return isok;
}

int modifyStudent()
{
    drawModifyStudent1();
    std::string userid;
    userid = readCommand();
    if(userid == "q") return -1;
    if(userid.empty()) return false;
    return drawModifyStudent2(userid);
}

void drawAddStudent()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Student Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                         Add Student                      |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|  \033[01;32mNew Student ID   \033[01;35m| \033[01;32mNew Student Name \033[01;35m|\033[01;32mNew Student Sexual \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|:                  |:                 |:                  |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);
}

void drawDeleteStudent()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Student Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                      Delete Student                      |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mStudent ID:                                               \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);
}

void drawSpecificStudent()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Student Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                 Check Specific Student                   |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mStudent ID:                                               \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);    
    moveCursor('r', 12);
}

void drawModifyStudent1()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                 Student Manage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|               Modify Student Information                 |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mStudent ID:                                               \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);    
    moveCursor('r', 12);
}

int drawModifyStudent2(std::string userid)
{
    system("clear");
    bool isok = 0;
    int index;
    std::vector< std::vector<std::string> >  tempfile = readFile(STU_PATH);
    std::vector< std::vector<std::string> >  file(3), input(3);
    file[0].push_back("Student ID");
    file[1].push_back("Student Name");
    file[2].push_back("Sexual");
    
    input[0].push_back("Student ID");
    input[1].push_back("Student Name");
    input[2].push_back("Sexual");
    input[0].push_back("");
    input[1].push_back("");
    input[2].push_back("");


    tempfile.erase(tempfile.begin() + 5);
    tempfile.erase(tempfile.begin() + 4);
    tempfile.erase(tempfile.begin() + 0);
    for(int i = 0; i < tempfile.size(); i++)
    {
        tempfile[i].erase(tempfile[i].begin());
    }
    for(int i = 0; i < tempfile[0].size(); i++)
    {
        if(tempfile[0][i] == userid)
        {
            for(int j = 0; j < tempfile.size(); j++)
            {
                file[j].push_back(tempfile[j][i]);
            }
            index = i;
            isok = true;
            break;
        }
        
    }
    if(!isok) return false;
    tempfile = readFile(STU_PATH);
    tempfile.erase(tempfile.begin());
    for(int i = 0; i < tempfile.size(); i++)
    {
        tempfile[i].erase(tempfile[i].begin());
    }
    int max_[3] = {0};
    int _max = 1;
    for(int i = 0; i < 3; i++)
    {
        max_[i] = file[i][0].size() > file[i][1].size() ? file[i][0].size() : file[i][1].size();
        max_[i] += 10;
        _max += max_[i] + 1;
    }

    std::vector<std::string> title_list_1, title_list_2, title_list_3;
    title_list_1.push_back("Student Manage System");
    title_list_1.push_back("Modify Student Information");
    title_list_1.push_back("");
    title_list_1.push_back("Old Student Information");

    title_list_2.push_back("New Student Information");

    title_list_3.push_back("Press Enter to continue...");
    for (int i = 0; i < title_list_1.size(); i++)
    {

        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";

        for (int j = 0; j < ((_max - 2) / 2) - (title_list_1[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m" << title_list_1[i] << "\033[0m";

        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list_1[i].size() - title_list_1[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for (int i = 0; i < file[0].size(); i++)
    {
        for(int j = 0; j < file.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (file[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << file[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (file[j][i].size() - file[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }

    for (int i = 0; i < title_list_2.size(); i++)
    {

        for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";

        for (int j = 0; j < ((_max - 2) / 2) - (title_list_2[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m" << title_list_2[i] << "\033[0m";

        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list_2[i].size() - title_list_2[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    for (int i = 0; i < input[0].size(); i++)
    {
        for(int j = 0; j < input.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (input[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << input[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (input[j][i].size() - input[j][i].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }

    for (int i = 0; i < title_list_3.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";

        for (int j = 0; j < ((_max - 2) / 2) - (title_list_3[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m" << title_list_3[i] << "\033[0m";

        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list_3[i].size() - title_list_3[i].size() / 2); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }
    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    std::cout << "\033[01;32m";
    for(int i = 0; i < 3; i++)
    {
        moveCursor('u', (i == 0) ? 4 : 1);
        int mov = (i == 0) ? 0 : (i == 1) ? max_[0] + 2 : (i == 2) ? max_[0] + max_[1] + 3 : 0;
        moveCursor('r', mov);
        tempfile[i][index] = readCommand();
        if(tempfile[i][index] == "q") return -2;
    }
    std::cout << "\033[0m";

    if(tempfile[2][index] != "male" && tempfile[2][index] != "female") return false;
    std::fstream f;
    f.open(STU_PATH, std::ios::out);
    for(int i = 0; i < tempfile[0].size(); i++)
    {
        std::string temp;
        for(int j = 0; j < tempfile.size(); j++)
        {
            temp.append("\"" + tempfile[j][i] + "\"");
            if(j != tempfile.size() - 1) temp.append(" ");
        }
        f << temp << std::endl;
    }
    f.close();

    return true;
    
}

int showSpecificStudent()
{
    bool isok = 0;

    drawSpecificStudent();
    std::string userid = readCommand();
    if(userid == "q") return -1;
    if(userid.empty()) return false;
    std::vector<std::string> ans;
    std::fstream file;
    file.open(STU_PATH, std::ios::in);
    while(!file.eof())
    {
        std::string line;
        std::vector<std::string> temp;
        getline(file, line);
        temp = split(line, ' ');
        if(temp[0] == userid)
        {
            isok = 1;
            ans = temp;
            break;
        }
    }
    if(isok)
    {
        system("clear");
        std::vector<std::string> label_list, title_list;
        label_list.push_back("User ID");
        label_list.push_back("User Name");
        label_list.push_back("Sexual");
        label_list.push_back("Subjects");
        label_list.push_back("Credits");
        title_list.push_back("Student Manage System");
        title_list.push_back("Student " + userid);
        int max_[5];
        for(int i = 0; i < 5; i++) max_[i] = std::max(label_list[i].size(), ans[i].size());
        int _max = 0;
        for(int i = 0; i < 5; i++) _max += max_[i];
        _max += 6;
        for (int i = 0; i < title_list.size(); i++)
        {

            for (int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
            std::cout << std::endl;

            std::cout << "\033[01;35m|\033[0m";
            for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++)
                std::cout << " ";
            std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
            for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++)
                std::cout << " ";
            std::cout << "\033[01;35m|\033[0m" << std::endl;
        }
        for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < 5; j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (label_list[j].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << label_list[j] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (label_list[j].size() - label_list[j].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        for(int j = 0; j < 5; j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (ans[j].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << ans[j] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (ans[j].size() - ans[j].size() / 2); k++)
            std::cout << " ";
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;

        std::string tail = "Press Enter To Go Back...";
        std::cout << "\033[01;35m|";
        std::cout << tail;
        for(int i = 0; i < (_max - 2 - tail.size()); i++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
        for(int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
        char *p = getpass("");
        return isok;
    } 
    return isok;
}

bool Stu_isValid(std::string username, std::string userid)
{
    std::fstream file;
    std::string temp;
    file.open(STU_PATH, std::ios::in);
    while(!file.eof()) 
    {
        getline(file, temp);
        std::vector<std::string> lines = split(temp, ' ');
        if(lines[0] == userid || lines[2] == username) return 0;
    }
    return 1;
}